package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.json.JsonOperation;
import com.magicode.cms.dao.ITvFilmPersonDao;
import com.magicode.cms.entiy.base.Dictionary;
import com.magicode.cms.entiy.base.Enterprise;
import com.magicode.cms.entiy.base.Picture;
import com.magicode.cms.entiy.base.Program;
import com.magicode.cms.entiy.base.ProgramPersonRela;
import com.magicode.cms.entiy.base.RepresentWorks;
import com.magicode.cms.entiy.base.Tag;
import com.magicode.cms.entiy.base.TvFilmPerson;
import com.magicode.cms.service.ITvFilmPersonService;

@Service("servicetvFilmPerson")
public class TvFilmPersonServiceImpl extends BaseServiceImpl<TvFilmPerson, ITvFilmPersonDao> implements ITvFilmPersonService {

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "titleCn");
		map.put(json_title, "中文名称");
		map.put("width", 80);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "titleEn");
		map.put(json_title, "英文名称");
		map.put("width", 80);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "country");
		map.put(json_title, "国籍");
		map.put("width", 80);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "sex");
		map.put(json_title, "性别");
		map.put("width", 80);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "mainExp");
		map.put(json_title, "主要成就");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "programNames");
		map.put(json_title, "相关节目");
		map.put("width", 160);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public List<JsonOperation> getOpts(String url,String id) {
		List<JsonOperation> opts = new ArrayList<JsonOperation> ();
		JsonOperation opt= new JsonOperation();
		opt.setName("修改");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/getinfo/"+id+"/edit"+url);
		opt.setType("edit");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("删除");
		opt.setUrl(getCtx().getContextPath()+"/"+url+"/"+id+"?_requestMethod=delete");
		opt.setType("delete2");
		opts.add(opt);
		opt= new JsonOperation();
		opt.setName("预览");
		opt.setUrl(getCtx().getContextPath()+"/p/tvFilmPerson/sec/" + id);
		opt.setType("preview");
		opts.add(opt);
		return opts;
	}
	
	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql =" from " +TvFilmPerson.class.getName()+" "+entity_alias_name+" ";
		hql += " left join "+entity_alias_name+".country ";
		hql += " left join "+entity_alias_name+".mainPic ";
		
		if(null!=querywhere && !querywhere.isEmpty() && querywhere.containsKey("type")){
			hql = hql+" , "+Dictionary.class.getName()+" tp ";
		}
		if(null!=querywhere && !querywhere.isEmpty()){
			hql += "where 1 = 1 ";
			if(querywhere.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+querywhere.get(field_id)+"'";
			}
			if(querywhere.containsKey("titleCn")){
				hql = hql+" and "+entity_alias_name+".titleCn like '%"+querywhere.get("titleCn")+"%'";
			}
			if(querywhere.containsKey("titleEn")){
				hql = hql+" and "+entity_alias_name+".titleEn like '%"+querywhere.get("titleEn")+"%'";
			}
			if(querywhere.containsKey("country")){
				hql = hql+" and "+entity_alias_name+".country = '"+querywhere.get("country")+"'";
			}
			if(querywhere.containsKey("sex")){
				hql = hql+" and "+entity_alias_name+".sex = '"+querywhere.get("sex")+"'";
			}
			if(querywhere.containsKey("type")){
				hql = hql+" and tp.id = '"+querywhere.get("type")+"' and tp in elements("+entity_alias_name+".types) ";
			}
			if(querywhere.containsKey("mainExp")){
				hql = hql+" and "+entity_alias_name+".mainExp like '%"+querywhere.get("mainExp")+"%'";
			}
		}
		return hql;
	}
	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			hql += "order by "+entity_alias_name+".createTime desc ";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put("titleCn", ""+entity_alias_name+".titleCn as titleCn ");
			gridsel.put("titleEn", ""+entity_alias_name+".titleEn as titleEn ");
			gridsel.put("country", ""+entity_alias_name+".country.title as country ");
			gridsel.put("mainExp", ""+entity_alias_name+".mainExp as mainExp ");
//			gridsel.put("apdc", ""+entity_alias_name+".baseAttentionDegree as apdc ");
			gridsel.put("sex", ""+entity_alias_name+".sex.title as sex ");
			List<Map<String, Object>> resultList = this.queryMapDataList(hql, gridsel,start , size);
			if(null!=resultList &&resultList.size()>0){
				//关联节目
				hql = "select distinct new map(t.id as id , d.program.titleCn as title) from "+ProgramPersonRela.class.getName()+" d,  " +TvFilmPerson.class.getName()+" t "
						+ "where d in elements(t.programList) ";
				String propertyName = "programNames";
				resultList =  this.getRelateProByList(hql, ",", propertyName, resultList);
			}
			return resultList;
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public ITvFilmPersonDao getDao() throws Exception {
		return tvFilmPersonDao;
	}

	@Autowired
	@Qualifier("daotvFilmPerson")
	private ITvFilmPersonDao tvFilmPersonDao;

	public ITvFilmPersonDao getTvFilmPersonDao() {
		return tvFilmPersonDao;
	}
	public void setTvFilmPersonDao(ITvFilmPersonDao tvFilmPersonDao) {
		this.tvFilmPersonDao = tvFilmPersonDao;
	}

	@Override
	public List<Tag> getTags(String id) {
//		请修改此方法，查询Tag，无需查处TvFilmPerson对象fetch出其他对象的同时查询Tag；
//		只需执行一个sql查询出关联TvFilmPerson的Tag即可
		List<Tag> tagList = new ArrayList();
		String sql = " select p from "+Tag.class.getName()+" p , "+TvFilmPerson.class.getName()+" t "
				+ " where p in elements(t.tags) and t.id = :id order by p.id ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try{
			tagList = this.tvFilmPersonDao.getCommonDaoService().queryList(sql, map);
		} catch (Exception e1){
			e1.printStackTrace();
		}
		return tagList;
	}

	/**
	 * 查询作品年表列表
	 */
	@Override
	public List<RepresentWorks> getRepresentWorksList(String id) {
		List<RepresentWorks> list = new ArrayList<RepresentWorks>();
		String sql = " select p from "+RepresentWorks.class.getName()+" p , "+TvFilmPerson.class.getName()+" t "
				+ " where p in elements(t.representWorksList) and t.id = :id order by p.id ";
		Map<String,String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			list = this.tvFilmPersonDao.getCommonDaoService().queryList(sql, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<Picture> getPictures(String id) {
		List<Picture> picList = new ArrayList();
		String sql = " select p from "+Picture.class.getName()+" p , "+TvFilmPerson.class.getName()+" t "
				+ " where p in elements(t.relaPics) and t.id = :id order by p.id ";
		TvFilmPerson tvFilmPerson = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			picList = this.tvFilmPersonDao.getCommonDaoService().queryList(sql, map);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		return picList;
	}
	
	@Override
	public List<ProgramPersonRela> getprogramList(String id) {
		
		List<ProgramPersonRela> programList = new ArrayList();
		String sql = " select ppr from "+ProgramPersonRela.class.getName()+" ppr left outer join fetch ppr.program p left join ppr.person t "
				+ " where t.id = :id order by ppr.id ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try{
			programList = this.tvFilmPersonDao.getCommonDaoService().queryList(sql, map);
		} catch (Exception e1){
			e1.printStackTrace();
		}
		return programList;
	}
	
	@Override
	public List<Map<String,Object>> simpleSelect(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_titleCn, ""+entity_alias_name+".titleCn as text ");
			return this.queryMapDataList(hql, gridsel,0 , 50);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public void deletePic(String picId,String personId) {
		this.tvFilmPersonDao.deletePic(picId,personId);
	}

	@Override
	public List<TvFilmPerson> list() {
		String hql = " from " + TvFilmPerson.class.getName() + " t where 1=1 and t.initialWordDic is null";
		List<TvFilmPerson> list = this.tvFilmPersonDao.getCommonDaoService().getListByHQL(hql);
		return list;
	}
	
	

}
